#ifndef __RADAR_PROTOCOL_H__
#define __RADAR_PROTOCOL_H__

#include <stdint.h>
#include <stdbool.h>
#include "queue.h"
#include "radar_protocol_blg.h"
#include "radar_protocol_sstk.h"
#include "video_protocol.h"

enum {
    RADAR_INCOMING = 0,
    RADAR_OUTGOING,
};

enum {
    RADAR_SERVER_MODE = 0,
    RADAR_CLIENT_MODE,
};

enum {
    RADAR_TYPE_BLG = 1,
    RADAR_TYPE_SSTK = 2,
};

typedef struct {
    uint16_t id;
    int8_t lane;
    uint8_t type;
    uint8_t length;
    int16_t dist_x;
    int16_t dist_y;
    int16_t speed_x;
    int16_t speed_y;
    int16_t speed;
    double longitude;
    double latitude;
    int16_t height;
    double speed_longitude;
    double speed_latitude;
    int8_t row;
    int8_t col;
} radar_obj_info_t;

typedef struct {
    radar_obj_info_t obj_info[RADAR_INFO_SIZE];
    uint16_t obj_num;
    int64_t frame_time;
    uint8_t time_stamp[8];
    Bitmap obj_map;
} radar_frame_t;

typedef struct {
    uint16_t twin1;
    uint16_t twin2;
} twin_pair_t;

typedef struct {
    uint16_t id;
    uint8_t dev_type;
    uint8_t obj_type;
    int8_t report_lane;
    int8_t current_lane;
    int64_t change_time;

    int16_t dist_x;
    int16_t dist_y;

    int64_t last_speed_time;
    int16_t last_speed;
    int16_t current_speed;

    int16_t acceleration;
} obj_static_t;

typedef struct {
    uint16_t id;
    int16_t dist_x;
    int16_t dist_y;
    int64_t time_stamp;
} false_obj_t;

typedef struct
{
    Queue_t *in_radar_frame_queue;
    Queue_t *in_new_obj_queue;
    Queue_t *out_new_obj_queue;
    Queue_t *out_radar_frame_queue;
    Queue_t *false_targets_queue;
    Queue_t *twin_pair_queue;
    Queue_t *event_static_queue;
} radar_manager_t;

void radar_protocol_init(void);
void radar_protocol_deinit(void);
void analysis_radar_buffer(char *buffer, int length, uint8_t dev_type);
void print_frame_info(radar_frame_t *radar_frame);
void print_last_frame_info(void);
bool is_false_target(uint16_t id);
int8_t lane_to_index(uint8_t lane);
bool update_event_queue_blg(uint16_t id, uint16_t event_id, uint8_t dev_type);
void test_pole_relay(struct_info_t *struct_info);

bool update_new_enter_obj(radar_obj_info_t *radar_obj_info, int8_t dev_type);
bool filter_radar_obj(radar_obj_info_t *radar_obj_info, int8_t dev_type);
void clac_coordinate_speed(radar_obj_info_t *radar_obj_info, Queue_t *radar_queue);
void process_manager_overlaps(radar_manager_t *mgr);
bool event_lane_filter(uint8_t lane_id);
#endif  // !__RADAR_PROTOCOL_H__
